package sim.core;

/**
 * 
 * This is the base class for creating asynchronous periodic tasks in the simulator.
 * 
 * Typically this class will be used to create anonymous classes by overriding run() to
 * place the code to be executed at given times in the future.
 *
 * Periodic tasks repeat execution with a given period (frequency) until canceled. 
 * 
 * They can be re-scheduled (within run()) to execute again at a later time.
 * 
 * Periodic tasks can be canceled to prevent them from executing.
 * 
 * Periodic tasks are executed according to <b>simulation time</b>.
 * 
 * @author smd
 *
 */
abstract public class PeriodicTask extends Task {
    
    protected final double period ;
    
    /**
     * @param p
     */
    public PeriodicTask(double p) {
        this(0, p) ;
    }
    
    /**
     * @param d
     * @param p
     */
    public PeriodicTask( double d, double p) {
        super( d ) ;
        this.period = p ;
    }
    
    /**
     * @param owner
     * @param p
     */
    public PeriodicTask( Object owner, double p) {
        this(owner, 0, p) ;
    }
    
    /**
     * @param owner
     * @param d
     * @param p
     */
    public PeriodicTask( Object owner, double d, double p) {
        super(owner, d ) ;
        this.period = p ;
    }
    
    public void reSchedule() {
       if( ! reScheduled )
    	   super.reSchedule( period ) ;
    }
}
